﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Snake_Classic
{
    public partial class PlayerNameForm : Form
    {
        public PlayerNameForm()
        {
            InitializeComponent();
        }
        MenuForm menu;
        Form1 f1 = new Form1();
        /// <summary>
        /// get variable from first form
        /// </summary>
        /// <param name="data">variable from first form</param>
        public PlayerNameForm(int data)
        {
            InitializeComponent();
            this.ScoreVariable = data;
        }
        int ScoreVariable;
        string playerName;
       
        private void button1_Click(object sender, EventArgs e)
        {
            CreateXMLDocument();
            if (NametextBox.Text == "")
            {
                playerName = "player";
            }
            else
            {
                playerName = NametextBox.Text;
            }
            WriteToXMLFile(playerName, ScoreVariable.ToString());
            this.Close();

            Form1.ActiveForm.Close();
            menu = new MenuForm();
            menu.Show();
            
        }

        /// <summary>
        /// write name and score to xml file
        /// </summary>
        /// <param name="playerName">player name</param>
        /// <param name="score">his score in game</param>
        public void WriteToXMLFile(string playerName, string score)
        {
            try
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string Filepath = Path.Combine(directory, "Score.xml");
                XmlDocument xmlDocument = new XmlDocument();
                FileStream fs = new FileStream(Filepath, FileMode.Open);  
                xmlDocument.Load(fs);  
  
                // Create new record
                XmlElement HightScore = xmlDocument.CreateElement("HightScore");
  
                // Create 2 field's name and score 
                XmlElement Name = xmlDocument.CreateElement("name");  
                XmlElement Score = xmlDocument.CreateElement("score");
                // Create 2 record's name and score  
                XmlText tName = xmlDocument.CreateTextNode(playerName);  
                XmlText tScore = xmlDocument.CreateTextNode(score.ToString());

                Name.AppendChild(tName); // Add values  
                Score.AppendChild(tScore); // ЗAdd values 
       
                HightScore.AppendChild(Name);
                HightScore.AppendChild(Score);  
  
                // Add record's to file
                xmlDocument.DocumentElement.AppendChild(HightScore);  
  
                fs.Close();// Close stream 
                xmlDocument.Save(Filepath); // Save file 
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// Create xml file to store score
        /// </summary>
        private void CreateXMLDocument()  
        {
            //check is file exist or not
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string Filepath = Path.Combine(directory, "Score.xml");
            if (!File.Exists(Filepath))
            {
                //create new xml file 
                XmlTextWriter xtw = new XmlTextWriter(Filepath, Encoding.UTF8);
                xtw.WriteStartDocument();
                xtw.WriteStartElement("Score.xml");
                xtw.WriteEndDocument();
                xtw.Close();  
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1.ActiveForm.Close();
            menu = new MenuForm();
            menu.Show();
        }
    }
}
